<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\utils\AppResult;
use app\utils\DataUtil;
use app\utils\VueAdmin\AdminData;
use think\Request;

/**
 * Admin 模块父级
 * Module 接管 CURD 公共操作
 * Class ModuleController
 * @package App\Http\Controllers\Framework
 */
class VueModule extends Backend
{
    protected       $model;  // 在子类中赋值
    protected array $params       = [];    // 请求参数
    protected array $unset_fields = ['status'];    // 初始化隐藏字段
    protected int   $statusValue  = 1;    // 状态默认值
    protected bool  $isPost;              // 请求类型是否为POST

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->isPost = $this->request->isPost();
        $this->params = $this->request->param();
        // 前置操作
        $this->callAction($this->request->action(), $this->request->param());
    }

    /**
     * Execute an action on the controller.
     *
     * @param $method
     * @param $parameters
     * @return mixed
     */
    public function callAction($method, $parameters = [])
    {
        return $this;
    }

    /**
     * 列表页
     * @return string|\think\response\Json
     * @throws \think\Exception
     */
    public function index()
    {
        if ($this->isPost) {
            $params = $this->params;

            $query = $this->model;
            // 自动排序
            if ($orderBy = AdminData::orderBy()) {
                foreach ($orderBy as $order) {
                    $query->order($order['sort'], $order['orderBy']);
                }
            }

            if (!isset($params['page'])) {
                $list = $query->select();
            } else {
                $list = $query->paginate($params['size'] ?? 15);
            }

            return AppResult::response200('success', $list);
        } else {
            return $this->view->fetch();
        }
    }

    /**
     * 添加
     * @return string
     */
    public function insert()
    {
        $params = $this->params;

        // 初始化空数据
        $form                = DataUtil::field($params, $this->unset_fields);
        $form['status']      = $this->statusValue;
        $form['create_time'] = time();
        if (!$this->model->insert($form)) {
            return AppResult::response201('创建失败');
        }
        return AppResult::response200('创建成功');
    }

    /**
     * 修改
     * @return string
     */
    public function update()
    {
        $params = $this->params;
        if (empty($params['id'])) {
            return AppResult::response203('操作超时！');
        }

        // 初始化空数据
        $form                = DataUtil::field($params, array_merge(['is_system'], $this->unset_fields));
        $form['update_time'] = time();
        if (!$this->model->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }

    /**
     * 状态
     * @return string
     */
    public function status()
    {
        $params = $this->params;
        if (empty($params['id']) || !isset($params['status'])) {
            return AppResult::response203('操作超时！');
        }

        $form['status']      = $params['status'] == 1 ? 2 : 1;
        $form['update_time'] = time();
        if (!$this->model->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }

    /**
     * 删除
     * @return string
     */
    public function remove()
    {
        $params = $this->params;
        if (empty($params['id'])) {
            return AppResult::response203('操作超时！');
        }

//        $form['delete_time'] = time();
//        $form['update_time'] = time();
//        if (!$this->model->where('id', $params['id'])->update($form)) {
//            return AppResult::response201('操作失败');
//        }
        if (!$this->model->where('id', $params['id'])->delete()) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }

    /**
     * 排序
     * @return string
     */
    public function rank()
    {
        $params = $this->params;
        if (!isset($params['id']) || !isset($params['weigh'])) {
            return AppResult::response203('操作超时！');
        }

        $form['weigh']       = $params['weigh'];
        $form['update_time'] = time();

        if (!$this->model->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }

        return AppResult::response200('操作成功');
    }
}
